package com.xspace.android.xafcommon.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xspace.android.xafcommon.R;

/**
 * Wubj 创建于 2017/3/22 0022.
 */
public class FloatOperateButton extends RelativeLayout {

    private RelativeLayout mRelativeOperate;
    private RelativeLayout mRelativeComb;
    private ImageView mIvOperate;
    private TextView mTvOperate;
    private TextView mTvCenter;

    public FloatOperateButton(Context mContext) {
        super(mContext);
        init(mContext, null);
    }

    public FloatOperateButton(Context mContext, AttributeSet attrs) {
        super(mContext, attrs);
        init(mContext, attrs);
    }

    private void init(Context mContext, AttributeSet attrs) {
        LayoutInflater.from(mContext).inflate(R.layout.float_button_layout, this, true);
        mRelativeOperate = (RelativeLayout) findViewById(R.id.relative_float_operate); //根部局
        mRelativeComb = (RelativeLayout) findViewById(R.id.relative_pic_and_tv); //图片和文字
        mIvOperate = (ImageView) findViewById(R.id.iv_float_operate); //组合模式图片
        mTvOperate = (TextView) findViewById(R.id.tv_float_operate); //组合模式文字
        mTvCenter = (TextView) findViewById(R.id.tv_center); //纯文字模式的文字

        parseStyle(mContext, attrs);
    }

    private void parseStyle(Context mContext, AttributeSet attrs) {
        if (attrs != null) {
            TypedArray ta = mContext.obtainStyledAttributes(attrs, R.styleable.FloatOperate);
            String title = ta.getString(R.styleable.FloatOperate_floatOperateText);
            if (mTvOperate != null) {
                mTvOperate.setText(title);
            }
            Drawable leftDrawable = ta.getDrawable(R.styleable.FloatOperate_floatOperateImage);
            if (null != leftDrawable) {
                mIvOperate.setImageDrawable(leftDrawable);
            }
            Drawable background = ta.getDrawable(R.styleable.FloatOperate_floatOperateBackground);
            if (null != background) {
                mRelativeOperate.setBackgroundDrawable(background);
            }
            ta.recycle();
        }
    }

    public void setImageView(int resId) {
        mIvOperate.setImageResource(resId);
    }

    public void setTextView(String str) {
        mTvOperate.setText(str);
    }

    public void setLayoutBackGround(int resId) {
        mRelativeOperate.setBackgroundResource(resId);
    }

    public void setLayoutBackGround(Drawable background) {
        mRelativeOperate.setBackgroundDrawable(background);
    }

    public void setImageVisible(int visible) {
        mIvOperate.setVisibility(visible);
    }

    public void setTextMode(String text) {
        mRelativeComb.setVisibility(View.GONE);
        mTvCenter.setVisibility(View.VISIBLE);
        mTvCenter.setText(text);
    }

    public void setOnlyShowText(String text) {
        mIvOperate.setVisibility(GONE);
        mTvOperate.setText(text);
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mTvOperate.getLayoutParams();
        params.addRule(RelativeLayout.CENTER_IN_PARENT);
        mTvOperate.setLayoutParams(params);
    }

    public TextView getTextView() {
        return mTvOperate;
    }

}
